﻿using Brjnk.Common;
using Brjnk.GpxExplorer.Data.GPX.SerializationClasses;

namespace Brjnk.GpxExplorer.Data.GPX
{
    public class GpxTrackSegment : TrackSegment, IGpxObject
    {
        private trksegType data;

        public GpxTrackSegment(trksegType data)
        {
            this.data = data;
            Points = new MVVM.ObservableCollectionEx<TrackPoint>();

            foreach (var wp in data.trkpt)
            {
                Points.Add(new GpxTrackPoint(wp));
            }
        }

        public GpxTrackSegment() : this(new trksegType()) { }

        public object GetUnderlayingObject()
        {
            data.trkpt.Clear();
            foreach (var p in Points)
            {
                wptType wp;
                if (p is GpxTrackPoint)
                {
                    wp = (wptType)(p as GpxTrackPoint).GetUnderlayingObject();
                }
                else
                {
                    wp = (wptType)p.ConvertTo<GpxTrackPoint>().GetUnderlayingObject();
                }
                data.trkpt.Add(wp);
            }
            return data;
        }

        public override object Clone()
        {
            return new GpxTrackSegment((trksegType)DeepClone.Clone(this.GetUnderlayingObject()));
        }
    }
}